package com.xingkeduo.business.operation.domain;

import com.xingkeduo.business.operation.service.PermsService;
import com.xingkeduo.business.operation.service.PostsPermsService;
import com.xingkeduo.pojo.query.PermsQuery;
import com.xingkeduo.pojo.query.PostsPermsQuery;
import com.xingkeduo.pojo.table.PermsTable;
import com.xingkeduo.pojo.table.PostsPermsTable;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 商户domain
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Component
public class PostsPermsDomain {

    @Autowired
    private PostsPermsService postsPermsService;

    @Autowired
    private PermsService permsService;

    public PostsPermsTable getPostsPermsById(Long id) throws Throwable {

        PostsPermsTable postsPermsTable = postsPermsService.selectById(id);

        return postsPermsTable;
    }

    public List<PermsTable> getPostsPermsByPosts(PostsPermsQuery postsPermsQuery) throws Throwable {
        PermsQuery permsQuery = new PermsQuery();

        List<PermsTable> permsTableList  = permsService.selectLists(permsQuery);

        List<PermsTable> permsTableListTemp= new ArrayList<>();
        List<PostsPermsTable> postsPermsList = new ArrayList<>();
        if(!CommonUtil.isEmpty(postsPermsQuery.getPostsId())){
            postsPermsList= postsPermsService.selectLists(postsPermsQuery);
        }

        List<String> permsIdList = new ArrayList<>();
        if(!CommonUtil.isEmpty(postsPermsList) && !CommonUtil.isEmpty(postsPermsList.get(0))){
            PostsPermsTable postsPermsTable = postsPermsList.get(0);
            permsIdList =  CollectionUtils.arrayToList(postsPermsTable.getPermsIds().split(","));
        }

        for (PermsTable permsTable: permsTableList) {
            if(permsIdList.size() > 0){
                for (String permsId : permsIdList) {
                    Long id =  Long.valueOf(permsId);
                    if (id.longValue() == permsTable.getId()){
                        permsTable.setChecked(true);
                        break;
                    }else{
                        permsTable.setChecked(false);
                    }
                }
            }
            permsTableListTemp.add(permsTable);
        }

        return permsTableListTemp;
    }


    public List<PostsPermsTable> getPostsPermsList(PostsPermsQuery postsPermsQuery) throws Throwable {

        List<PostsPermsTable> postsPermsList = postsPermsService.selectLists(postsPermsQuery);

        return postsPermsList;
    }

    public int createPostsPerms(PostsPermsTable postsPermsTable) throws Throwable {



        return postsPermsService.insert(postsPermsTable);
    }

    public int modifyPostsPerms(PostsPermsTable postsPermsTable) throws Throwable {
        PostsPermsQuery postsPermsQuery = new PostsPermsQuery();
        List<PostsPermsTable> postsPermsList = new ArrayList<>();

        if(!CommonUtil.isEmpty(postsPermsTable.getPostsId())){
            postsPermsQuery.setPostsId(postsPermsTable.getPostsId());
            postsPermsList= postsPermsService.selectLists(postsPermsQuery);

            if(!CommonUtil.isEmpty(postsPermsList) && !CommonUtil.isEmpty(postsPermsList.get(0))){
                PostsPermsTable postsPermsTableTemp = postsPermsList.get(0);
                postsPermsTable.setId(postsPermsTableTemp.getId());
                return postsPermsService.update(postsPermsTable);
            }else{
                return  this.createPostsPerms(postsPermsTable);
            }
        }else{
            return 0;
        }

    }

    //删除
    public int delPostsPerms(Long code) throws Throwable {
        PostsPermsTable postsPermsTable = new PostsPermsTable();

        postsPermsTable.setCode(code);
        postsPermsTable.setIsDelete(YesNoEnum.YES);
        int delRes = postsPermsService.delete(postsPermsTable);
        if(delRes > 0){
            return delRes;
        }else{
            throw new DataException(DataException.Type.FAIL, "删除失败");
        }
    }

}
